package cn.source.web.controller.zk;

import cn.source.common.core.controller.BaseController;
import cn.source.common.core.domain.AjaxResult;
import cn.source.common.core.domain.model.LoginUser;
import cn.source.common.core.page.TableDataInfo;
import cn.source.common.core.domain.entity.Customer;
import cn.source.common.utils.ServletUtils;
import cn.source.common.utils.poi.ExcelUtil;
import cn.source.framework.web.service.TokenService;
import cn.source.zk.domain.entity.Device;
import cn.source.zk.domain.vo.DeviceChannelVO;
import cn.source.zk.service.IZkAreaService;
import cn.source.zk.service.IZkCustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/zk/customer")
public class ZkCustomerController extends BaseController {

    @Autowired
    private IZkCustomerService customerService;

    @Autowired
    private IZkAreaService areaService;

    @Autowired
    private TokenService tokenService;

    /**
     * 获取客户分页列表(仅正常客户)
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo pageList(Customer customer){
        startPage();
        List<Customer> list = customerService.pageList(customer);

        return getDataTable(list);
    }

    /**
     * 获取客户分页列表（所有客户）
     * @return
     */
    @GetMapping("/listAll")
    public TableDataInfo pageListALL(Customer customer){
        startPage();
        List<Customer> list = customerService.pageListALL(customer);

        return getDataTable(list);
    }



    /**
     * 新增客户
     * @param customer
     * @return
     */
    @PostMapping
    public AjaxResult saveCustomer(@RequestBody Customer customer){
        // 检查客户唯一性
        if (customerService.checkCustomerExist(customer.getPhone(),null)){
            return AjaxResult.error("手机号"+customer.getPhone()+"已存在，请确认客户信息");
        }

        customer.setCreateBy(getUsername());
        customer.setUpdateBy(getUsername());

        return toAjax(customerService.save(customer));
    }

    /**
     * 根据num查询用户
     * @param num
     * @return
     */
    @GetMapping("/{num}")
    public AjaxResult getByNum(@PathVariable Long num){
        Customer customer = customerService.getByNum(num);
        return AjaxResult.success(customer);
    }

    /**
     * 修改客户
     * @param customer
     * @return
     */
    @PutMapping
    public AjaxResult editCustomer(@RequestBody Customer customer){
        // 检查客户唯一性
        if (customerService.checkCustomerExist(customer.getPhone(),customer.getNum())){
            return AjaxResult.error("手机号"+customer.getPhone()+"已存在，请确认客户信息");
        }

        customer.setUpdateBy(getUsername());

        return toAjax(customerService.editCustomer(customer));
    }

    /**
     * 删除客户
     * @param nums
     * @return
     */
    @DeleteMapping("/{nums}")
    public AjaxResult deleteCustomer(@PathVariable List<Long> nums){
        return toAjax(customerService.del(nums));
    }

    /**
     * 导出客户
     * @param response
     * @param customer
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, Customer customer){
        List<Customer> list = customerService.pageList(customer);
        ExcelUtil<Customer> util = new ExcelUtil<Customer>(Customer.class);
        util.exportExcel(response, list, "客户信息");
    }

    /**
     * 导入客户
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<Customer> util = new ExcelUtil<Customer>(Customer.class);
        List<Customer> userList = util.importExcel(file.getInputStream());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = loginUser.getUsername();
        //String message = userService.importUser(userList, updateSupport, operName);
        return AjaxResult.success("111");
    }
}
